<!--
Template Name: 分页
Create author: qinglong
Create Time  : 2020-07-27
-->
<template>
  <div class="mix-page">
    <slot name="left">
      <div></div>
    </slot>
    <el-pagination background v-bind="$attrs" :current-page.sync="page.pageIndex" :layout="page.layout || 'total,sizes,prev,pager,next,jumper'" :page-size="page.pageSize" :page-sizes="sizes" :total="page.total || total" @size-change="sizeChange" @current-change="currentChange" />
    <slot name="right"></slot>
  </div>
</template>
<script>
export default {
  name: 'MixPage',
  props: {
    value: {
      type: Object,
      default() {
        return {
          pageIndex: 1,
          pageSize: 10,
          total: 0,
        };
      },
    },
    total: Number,
  },
  watch: {
    value: {
      deep: true,
      handler(val) {
        if (Math.ceil(val.total / val.pageSize) < val.page && val.page > 1) {
          val.page--;
          this.page = { ...val };
          this.currentChange();
        } else {
          this.page = { ...val };
        }
      },
    },
  },
  data() {
    return {
      page: {},
      sizes: [10, 20, 30, 50, 70, 90, 100],
    };
  },
  created() {
    this.page = { pageIndex: 1, pageSize: 10, ...this.value };
    // if (!this.sizes.includes(this.value.pageSize)) this.sizes.unshift(this.value.pageSize);
  },
  methods: {
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.page.page = 1;
      this.currentChange();
    },
    currentChange() {
      this.$emit('input', { ...this.page });
      if (typeof this.value.event == 'function') {
        this.value.event();
      } else if (typeof this.event == 'function') {
        this.event();
      } else {
        this.$parent.getData(this.page);
      }
    },
  },
};
</script>
<style lang="less" scoped>
.mix-page {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 20px;
}
</style>
